package com.itheima.leyou.web;

import com.itheima.leyou.common.dto.PageDTO;
import com.itheima.leyou.service.BrandService;
import com.itheima.leyou.service.CategoryService;
import com.itheima.leyou.vo.BrandVO;
import com.leyou.item.dto.BrandDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    private BrandService brandService;

    public BrandController(BrandService brandService){
        this.brandService=brandService;
    }

    @Autowired
    private CategoryService categoryService;
    /**
     * 根据品牌id查询所对应的品牌信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){
        return ResponseEntity.ok().body(brandService.queryBrandById(id));
    }


    /**
     * 根据品牌id查询所对应的品牌信息
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandById(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok().body(brandService.queryBrandByIds(ids));
    }


    /**
     * 根据分类id查询所对应的品牌信息
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long id){
        return ResponseEntity.ok().body(brandService.queryBrandByCategoryId(id));
      //  return ResponseEntity.ok().body(brandService.queryBrandByCategory(id));

    }


    /**
     * 分页查询品牌的信息
     * @param page  当前页码
     * @param rows 每页显示的条数
     * @param key 查询条件
     * @return
     */
//    @PostMapping("/page")
    //DTO response VO resqust
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> findPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page ,
            @RequestParam(value = "rows",defaultValue = "10")Integer rows ,
            @RequestParam(value = "key",required = false)String key
    ){
        PageDTO<BrandDTO> page1 = brandService.findPage(page, rows, key);
        return ResponseEntity.ok(page1);
    }


    /**
     * 新增品牌
     * @param brandDTO
     * @return
     *
     */
    @PostMapping
    public ResponseEntity<Void> add(BrandDTO brandDTO){
        brandService.addOrUpdate(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

//    @PostMapping
//    public ResponseEntity<Void> add(BrandVO brandVO){
//        brandService.addOrUpdate(brandVO);
//        return ResponseEntity.status(HttpStatus.CREATED).build();
//    }
    /**
     * 更新品牌
     * @param brandDTO
     * @return
     * http://api.leyou.com/item/category/of/brand/?id=2032
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.ok().build();
    }


    /**
     * 删除品牌信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> update(@PathVariable("id")Long id){
        Boolean flag = brandService.removeByPid(id);
       return ResponseEntity.status(HttpStatus.NO_CONTENT).body(flag);
    }
}
